#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<conio.h>
#include<cctype>
#include<fstream>
#include"query.h"

using namespace std;

class Database{
	private:
	Table* StartTable;  //dummy node
  vector<string> TableName;
  vector<string> names;
  vector< vector<FieldType> > types;
  vector<int> nAttribute;


	Table* FindTable(string);			                                          //gets a pointer to the table before the table of the same name
	Record* FindRecord(string,void*);	                                      //gets a pointer to the record before the record with a matching key
  vector<Record*> FindRecord(string TableName,string AttributeName,void* value);  //returns a vector of pointers
                                                                                  //Each pointer points to the record before
                                                                                  //the desired record.
	void InsertTable(Table*);                                               //insert a table at the beginning of the list
	
  FieldType getFieldType(string str);

	public:
	Query QueryObj;

	Database();
  ~Database();
	bool CreateTable(string, vector<string>&, vector<FieldType>&, int);       //Creates a table with a list of the names and a list of the
                                                                            //types of each attribute
	void DefineWith(string, string);

  void DeleteTable(string TableName);                                       //Deletes TableName

  void DeleteRecord(string From, string Where);                             //Removes a specific record

  void EmptyTable(string TableName);

  void UpdateTable(string Update, string Set, string wher);                 //Updates a record

  bool InsertRecord(string, vector<void*> &toInsert);                       //Inserts record toInsert by copying its data.

  void InsertRecord(string, string);

  void PrintTableInfo(string TableName)const;                               //

  void PrintRecordInfo(string TableName,ostream&sout);                                   //

  void PrintAll()const;                                                     //Print the database(for debugging)

  void PrintAllTableRecord(ostream&);        

  void SaveTable(string);
  
  void SaveData(string);
  
  string TypeToString(FieldType);

  int NumTables()const;                                                     //returns the number of tables in the database

  bool ReadTableFormat(ifstream& fin);
  bool ReadRecordFormat(ifstream& fin);
  void DefineDatabaseFromFile(string fname);
  void InsertDataFromFile(string fname);
  string ReadStringFromFile(ifstream&fin);
};